package com.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Slf4j
public abstract class BaseServiceImpl<M extends com.baomidou.mybatisplus.core.mapper.BaseMapper<T>, T> extends ServiceImpl<M, T> {

    /**
     * 检查数据是否存在
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param cinemaId 影院ID
     * @param searchDateGetter 搜索日期字段getter
     * @param cinemaIdGetter 影院ID字段getter
     * @return 是否存在数据
     */
    protected boolean checkDataExists(String startDate, String endDate, String cinemaId, 
                                    SFunction<T, String> searchDateGetter, SFunction<T, String> cinemaIdGetter) {
        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(searchDateGetter, startDate)
                .le(searchDateGetter, endDate)
                .eq(cinemaIdGetter, cinemaId);
        return count(queryWrapper) > 0;
    }

    /**
     * 删除指定日期范围的数据
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param cinemaId 影院ID
     * @param searchDateGetter 搜索日期字段getter
     * @param cinemaIdGetter 影院ID字段getter
     */
    protected void deleteDataByDateRange(String startDate, String endDate, String cinemaId,
                                       SFunction<T, String> searchDateGetter, SFunction<T, String> cinemaIdGetter) {
        LambdaUpdateWrapper<T> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.ge(searchDateGetter, startDate)
                .le(searchDateGetter, endDate)
                .eq(cinemaIdGetter, cinemaId);
        remove(updateWrapper);
    }

    /**
     * 处理数据
     * @param records 数据记录
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param forceUpdate 是否强制更新
     * @param cinemaId 影院ID
     * @param searchDateGetter 搜索日期字段getter
     * @param cinemaIdGetter 影院ID字段getter
     * @param entityList 实体列表
     * @return 处理成功的记录数
     */
    @Transactional
    protected int processData(List<Map<String, String>> records, String startDate, String endDate, boolean forceUpdate, String cinemaId,
                            SFunction<T, String> searchDateGetter, SFunction<T, String> cinemaIdGetter, List<T> entityList) {
        if (records == null || records.isEmpty()) {
            log.warn("No records to process");
            return 0;
        }

        // 如果不是强制更新，先检查数据是否存在
        if (!forceUpdate) {
            if (checkDataExists(startDate, endDate, cinemaId, searchDateGetter, cinemaIdGetter)) {
                log.info("数据已存在，startDate: {}, endDate: {}, cinemaId: {}, 跳过处理", startDate, endDate, cinemaId);
                return 0;
            }
        } else {
            // 如果是强制更新，先删除指定日期的数据
            deleteDataByDateRange(startDate, endDate, cinemaId, searchDateGetter, cinemaIdGetter);
        }

        if (!entityList.isEmpty()) {
            return saveBatch(entityList) ? entityList.size() : 0;
        }
        return 0;
    }
} 